package com.zyk.scaffold.oauth.config.oauth.support;

import com.zyk.scaffold.oauth.domain.LocalTenantUserDetails;
import com.zyk.scaffold.oauth.framework.domain.UserDetails;
import com.zyk.scaffold.oauth.framework.interfaces.IUserDetailsService;
import com.zyk.scaffold.user.dto.TenantUserInfoDTO;
import com.zyk.scaffold.user.rpc.UserRpcService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class LocalUserDetailsService implements IUserDetailsService {

    @DubboReference
    private UserRpcService userRpcService;

    @Override
    public UserDetails loadUserDetailsByAccount(String account) {
        return loadTenantUserDetails(account, null);
    }

    public UserDetails loadTenantUserDetails(String account, String tenantCode) {
        TenantUserInfoDTO tenantUserInfoDTO = userRpcService.getTenantUserApis(account, tenantCode);
        if(tenantUserInfoDTO == null){
            return null;
        }
        LocalTenantUserDetails localTenantUserDetails = new LocalTenantUserDetails(tenantUserInfoDTO);
        return localTenantUserDetails;
    }
}
